package com.project.dao.impl;

import com.project.bean.CutPageBean;
import com.project.bean.OrderBean;
import com.project.dao.IOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.time.LocalDate;

@Repository
public class IOrderDaoImpl extends BaseDao implements IOrderDao {

    /**
     * 分页
     *
     * @param pageNo 页码
     * @return 分页对象
     */
    @Override
    public CutPageBean<OrderBean> cutAll(int pageNo) {
        CutPageBean<OrderBean> cutBean = new CutPageBean<OrderBean>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from o_order limit ?,?");
            ps.setObject(1, (pageNo - 1) * CutPageBean.PAGESIZE);
            ps.setObject(2, CutPageBean.PAGESIZE);

            re = ps.executeQuery();
            while (re.next()) {
                OrderBean orderBean = this.getOrder();
                cutBean.getList().add(orderBean);
            }

            ps = con.prepareStatement("select count(*) c from o_order");
            re = ps.executeQuery();
            if (re.next()) {
                cutBean.setCount(re.getInt("c"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            this.closeConnection();
        }
        return cutBean;
    }

    private OrderBean getOrder() throws SQLException {
        OrderBean order = new OrderBean();
        order.setId(re.getInt("pk_orderId"));

        order.setUserName(re.getString("o_name"));
        order.setOrderDate(LocalDate.parse(re.getString("o_orderDay")));
        order.setAddress(re.getString("o_address"));
        return order;
    }

    /**
     * 添加订单
     *
     * @param order 订单对象
     */
    @Override
    public void add(OrderBean order) {

        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO o_order(o_name,o_orderDay,o_address)VALUES(?,?,?)");
            ps.setObject(1, order.getUserName());
            ps.setObject(2, order.getOrderDate());
            ps.setObject(3, order.getAddress());

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.closeConnection();
    }

    /**
     * 删除订单
     *
     * @param id 订单编号
     */
    @Override
    public void del(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete from o_order where pk_orderId=?");
            ps.setObject(1, id);

            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.closeConnection();
    }

    /**
     * 修改订单
     *
     * @param id      订单编号
     * @param address 新送货地址
     */
    @Override
    public void update(int id, String address) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update o_order set o_address = ? where pk_orderId = ?");
            ps.setObject(1, address);
            ps.setObject(2, id);

            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.closeConnection();
    }

    /**
     * 查找订单
     *
     * @param id 订单编号
     * @return
     */
    @Override
    public OrderBean findById(int id) {
        OrderBean orderBean = new OrderBean();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from o_order where pk_orderId = ?");
            ps.setObject(1, id);

            re = ps.executeQuery();
            while (re.next()) {
                orderBean.setId(re.getInt("pk_orderId"));
                orderBean.setUserName(re.getString("o_name"));
                orderBean.setOrderDate(LocalDate.parse(re.getString("o_orderDay")));
                orderBean.setAddress(re.getString("o_address"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderBean;
    }

    public static void main(String[] args) {
        IOrderDao dao =new IOrderDaoImpl();
        System.out.println(dao.findById(1));
    }
}

